﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rocket.Core;
using Rocket.Data;
using Rocket.Bll.Models;
using Rocket.Core.Common;

namespace Rocket.Bll
{
    public class HandlerSummaryView : LoggerBase, IViewHandler<SummaryView>
    {
        private HandlerQuestion handlerQuestion = new HandlerQuestion();
        private HandlerReaderApplication handlerReaderApplication = new HandlerReaderApplication();
        private HandlerReaderApplicationQuestion handlerReaderAppQuestions;
        protected override Type LogPrefix
        {
            get { return this.GetType(); }
        }

        public SummaryView GetView()
        {
            try
            {
                // YES YOU WOULD HAVE WRITTEN A SP TO DO THIS
                SummaryView summary = new SummaryView();
                summary.TotalQuestions = handlerQuestion.GetAll().Count.ToString();
                List<ReaderApplicationView> readerApps = handlerReaderApplication.GetAll();
                summary.TotalApplications = readerApps.Count.ToString();
                int maxQuestion = 0;
                string appMaxName = "";
                string appZeroName = "";
                foreach (ReaderApplicationView readerApp in readerApps)
                {
                    handlerReaderAppQuestions = new HandlerReaderApplicationQuestion(readerApp.Code);
                    int count = handlerReaderAppQuestions.GetAll().Count;
                    if (count > maxQuestion)
                    {
                        maxQuestion = count;
                        appMaxName = readerApp.Name;
                    }
                    if (count == 0)
                        appZeroName += readerApp.Name + ", ";
                }
                summary.ApplicationWithHeighestQuestions = appMaxName + " " + "(" + maxQuestion + " Questions)";
                summary.ApplicationWithNoQuestions = appZeroName.TrimEnd(new char[] { ' ', ',' });
                return summary;
            }
            catch (Exception e)
            {
                this.LogError("Error loading the summary view", e);
                return null;
            }
        }
    }
}
